package com.nsoft.html5npub.utils;

import java.util.ArrayList;
import java.util.List;

import com.nsoft.html5npub.R;
import com.nsoft.html5npub.objects.BookItem;
import com.nsoft.html5npub.objects.BookRange;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Gallery;

@SuppressWarnings("deprecation")
public class SingleGridArrayAdapter extends 
						ArrayAdapter<BookRange> {
	private Context context;
	private List<BookRange> bookRangeList;
	private Gallery parent;
	private int[] gridSize;
	
	public SingleGridArrayAdapter(Context ctx,
			List<BookRange> list,Gallery _parent,
			int[] _gridSize){
		super(ctx,R.layout.bookgrid_view,list);
		this.context=ctx;
		this.bookRangeList=list;
		this.parent=_parent;
		this.gridSize=_gridSize;
	}
	
	@Override
	public View getView(int position, View convertView, 
									ViewGroup parent){ 
		LayoutInflater inflater=(LayoutInflater)context
				.getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(
				R.layout.bookgrid_view,parent,false);
		
		SingleGridView gridView=(SingleGridView)view.
					findViewById(R.id.singleShelfGrid);
		gridView.setGalleryParent(this.parent);
		gridView.setColumnWidth(this.gridSize[3]);
		
		BookRange range=this.bookRangeList.get(position);
		List<BookItem> bookSubList=
					new ArrayList<BookItem>();
		for (int i=range.startIndex;
					i<=range.endIndex;i++){
			bookSubList.add(engine.getBookItem(i));
		}
		
		gridView.setAdapter(new BookGridAdapter(
					this.context,bookSubList,range,
					this.gridSize));
		
		return view;
	}
}
